{$CLEO .cs}
{$USE CLEO+}
{$USE audio}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "Strgt42"

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

0AF2: 28@ = get_float_from_ini_file "cleo\startconfig.ini" section "settings" key "Volume"

const
    Check = 21@
    engine_stat = 31@
    Check_Global = 30@
    car_x = 29@
    animat = 27@
    animat_check = 23@
    force = 24@
    try_again = 25@
    percent_off = 26@
    radio_off = 22@
end

0AF2: force = get_float_from_ini_file "cleo\startconfig.ini" section "Broken engine" key "Impact force"
0AF0: percent_off = read_int_from_ini_file "cleo\startconfig.ini" section "Broken engine" key "percent"

if
    0E20: is_on_samp
then
    0A93: terminate_this_custom_script
end

while true
wait 0
if
   Player.Defined($PLAYER_CHAR)
then
    if and
        8741:   not actor $PLAYER_ACTOR busted
        not actor.Dead($PLAYER_ACTOR)
        Actor.Driving($PLAYER_ACTOR)
        $ONMISSION == 0
    then
        0@ = Actor.CurrentCar($PLAYER_ACTOR)
        0227: 1@ = car 0@ health
        if and
            1@ > 250
            1@ < 500
        then
            0E65: get_car_collision_intensity 0@ store_to 1@
            if
                0025:   1@ > force  // (float)
            then
                if
                    0E4D: random_percent percent_off
                then

                    0085: car_X = 0@ // (int)

                    0AC1: 14@ = load_audio_stream_with_3d_support "CLEO/SoundsQa/STARTER.mp3" // IF and SET
                    0AC5: link_3d_audio_stream 14@ to_car 0@

                    engine_stat = 0
                    animat = 0.4
                    animat_check = false
                    try_again = 0
                    
                    051E: radio_off = get_current_radio_station

                    repeat
                    wait 0
                    04ED: load_animation  "DrIVeMod"
                    until 04EE:   animation "DrIVeMod" loaded

                        timera = 0
                        repeat
                        wait 0

                        gosub @Check_Global
                            if and
                                003B:   car_X == 0@  // (int)
                                Check_Global == true
                            then
                            
                                0227: 1@ = car 0@ health
                                if
                                    1@ <= 250
                                then
                                    break
                                end
                            
                                if and
                                    0ABE:   car 0@ engine_on
                                    engine_stat == 0
                                then
                                    engine_stat = 1
                                    0ABF: set_car 0@ engine_state_to 0
                                    041E: set_radio_station 12
                                end


                                if
                                    engine_stat == 1
                                then
                                    gosub @Animation_on_off
                                    engine_stat = 2
                                    0AAD: set_audio_stream 14@ state 1
                                    0ABC: set_audio_stream 14@ volume 28@
                                    timera = 0
                                end

                                if
                                    engine_stat == 2
                                then
                                    041E: set_radio_station 12
                                    0ABF: set_car 0@ engine_state_to 0
                                    if animat_check == false
                                    then animat += 0.01
                                    end
                                    if animat_check == true
                                    then animat -= 0.01
                                    end

                                    if animat > 0.65
                                    then animat_check = true
                                    end
                                    if animat < 0.4
                                    then animat_check = false
                                    end


                                    if or
                                        Actor.Animation($PLAYER_ACTOR) == "startgta4LOWG"
                                        Actor.Animation($PLAYER_ACTOR) == "startgta4KartG"
                                        Actor.Animation($PLAYER_ACTOR) == "startgta4garage"
                                    then
                                        if Actor.Animation($PLAYER_ACTOR) == "startgta4LOWG"
                                        then 0614: set_actor $player_actor animation "startgta4LOWG" progress_to animat
                                        end
                                        if Actor.Animation($PLAYER_ACTOR) == "startgta4KartG"
                                        then 0614: set_actor $player_actor animation "startgta4KartG" progress_to animat
                                        end
                                        if Actor.Animation($PLAYER_ACTOR) == "startgta4garage"
                                        then 0614: set_actor $player_actor animation "startgta4garage" progress_to animat
                                        end
                                    end

                                    if
                                        timera > 1800
                                    then
                                        if
                                            try_again >= 1
                                        then
                                            0ABF: set_car 0@ engine_state_to 1
                                            041E: set_radio_station radio_off
                                            break
                                        else
                                            engine_stat = 1
                                            try_again += 1
                                        end
                                    end

                                end


                            else
                                break
                            end
                        until not player.Defined(0)
                        0AAE: remove_audio_stream 14@
                        04EF: release_animation "DrIVeMod"
                    end
                end
            end
        end
    end
end

:Check_Global
Check_Global = false
    if and
        Actor.Driving($PLAYER_ACTOR)
        8741:   not actor $PLAYER_ACTOR busted
        not actor.Dead($PLAYER_ACTOR)
    then
        if and
            84C8:   not actor $PLAYER_ACTOR driving_flying_vehicle
            84A7:   not actor $PLAYER_ACTOR driving_boat
            84A9:   not actor $PLAYER_ACTOR driving_heli
            89AE:   not actor $PLAYER_ACTOR driving_train
        then
            0@ = Actor.CurrentCar($PLAYER_ACTOR)
            0E12: get_vehicle 0@ subclass_to 2@
            if or
                2@ == 1
                2@ == 0
            then
                Check_Global = true
            end
        end
    end
return


:Animation_on_off
if or
    Actor.Animation($PLAYER_ACTOR) == "CAR_Lsit"
    Actor.Animation($PLAYER_ACTOR) == "Drive_LO_L"
    Actor.Animation($PLAYER_ACTOR) == "Drive_LO_R"
then
    0605: actor $PLAYER_ACTOR perform_animation "startgta4LOWG" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
else
    if or
        Actor.Animation($PLAYER_ACTOR) == "KART_drive"
        Actor.Animation($PLAYER_ACTOR) == "KART_L"
        Actor.Animation($PLAYER_ACTOR) == "KART_R"
    then
        0605: actor $PLAYER_ACTOR perform_animation "startgta4KartG" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
    else
        0605: actor $PLAYER_ACTOR perform_animation "startgta4garage" IFP "DrIVeMod" framedelta 4.0 loop 0 lockX 0 lockY 0 lockF 0 time -1
    end
end
return

